<?php

namespace App\Http\Controllers;

use App\Models\DeliveryModel;
use App\Sevrice\ApiJwt;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DeliveryController extends Controller
{
    /**
     * @param Request $request
     * @return array
     * 新增收货地址信息
     */
    function deliveryInfoAdd(Request $request)
    {
        $userId = ApiJwt::decodeToken($request->post('token'));
        $count = DeliveryModel::where('user_num', $userId)->get()->count();
        if($count==0){
            $data['is_default']=1;
        }
        if ($count >= 10) {
            return ['code' => 400, 'msg' => '收货地址已达上限'];
        }
        $data = $request->post();
        $info = DeliveryModel::where('user_num', $userId)->where('consignee_phone', $data['consignee_phone'])
            ->where('consignee_name', $data['consignee_name'])->where('delivery_province', $data['delivery_province'])->first();
        if ($info) {
            return ['code' => 400, 'msg' => '此收货地址已存在，无需再次添加'];
        }
        $data['user_num'] = $userId;
        unset($data['token']);
        $res = DB::table('sf_delivery')->insert($data);
        if ($res) {
            return ['code' => 200, 'msg' => '添加成功'];
        }
        return ['code' => 400, 'msg' => '添加失败'];
    }

    /**
     * @param Request $request
     * @return mixed
     * 获取当前位置经纬度
     */
    function getAddress(Request $request){
        $address=$request->post('delivery_province');
        $data=json_decode(file_get_contents("https://restapi.amap.com/v3/geocode/geo?key=2abb737e70127ff9d2ee7958f6091b9c&address=$address"),true);
        return $data['geocodes'][0]['location'];
    }

    /**
     * @param Request $request
     * @return array
     * 查询用户默认收货信息
     */
    function deliveryInfo(Request $request){
        $userId = ApiJwt::decodeToken($request->get('token'));
        $deliveryId=$request->get('deliveryId');
        if($deliveryId){
            $data=DeliveryModel::where('delivery_id',$deliveryId)->first()->toArray();
        }else {
            $data = DeliveryModel::where('user_num', $userId)->where('is_default', 1)->first()->toArray();
        }
        return $data;
    }

    /**
     * @param Request $request
     * @return array
     * 查询用户收货地址信息
     */
    function deliveryInfoList(Request $request){
        $userId = ApiJwt::decodeToken($request->get('token'));
        $data=DeliveryModel::where('user_num',$userId)->get(['delivery_id','consignee_name','consignee_phone','delivery_province','is_default'])->toArray();
        return $data;
    }

    /**
     * @param Request $request
     * @return array
     * 更改用户收货地址默认状态
     */
    function editDelivery(Request $request){
        $userId = ApiJwt::decodeToken($request->post('token'));
        $deliveryId=$request->post('deliveryId');
        $res=DeliveryModel::where('delivery_id',$deliveryId)->update(['is_default'=>1]);
        if($res){
            $err=DeliveryModel::where('delivery_id','!=',$deliveryId)->where('user_num',$userId)->update(['is_default'=>0]);
            if($err){
                return ['code' => 200, 'msg' => '设置成功'];
            }
            return ['code' => 400, 'msg' => '设置失败'];
        }
        return ['code' => 400, 'msg' => '设置失败'];
    }

    /**
     * @param Request $request
     * @return mixed
     * 查询指定地址信息
     */
    function userDelivery(Request $request){
        $deliveryId=$request->get('deliveryId');
        $data=DeliveryModel::where('delivery_id',$deliveryId)->first(['consignee_name','delivery_id','consignee_phone','delivery_province'])->toArray();
        $first=strpos($data['delivery_province'],'-');
        $data['province']=substr($data['delivery_province'],0,$first);
        $second=strpos($data['delivery_province'],'-',$first+1);
        $third=strripos($data['delivery_province'],'-');
        $data['city']=substr($data['delivery_province'],($first+1),($second-$first)-1);
        $data['area']=substr($data['delivery_province'],($second+1),($third-$second)-1);
        $data['address']=substr($data['delivery_province'],$third+1);
        return $data;
    }
}
